<?php
namespace App\Http\Controllers\WebApi\User;

use App\Common\ErrorCode\ErrorCode;
use App\Exceptions\BusinessException;
use App\Http\Controllers\WebApi\BaseController;
use App\Http\Service\UserService;
use App\Http\Strategy\PersonalStrategy;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use App\Utilities\Upload;
use Hash;

class PersonalController extends BaseController
{
    private $personalStrategy;

    public function __construct(){
        //parent::__construct();

        $this->personalStrategy = new PersonalStrategy();
    }

    /**
     * 设置员工类型
     * @param Request $request
     * @throws BusinessException
     */
    public function setStaffType(Request $request)
    {
        $this->webParamValidate(
            $request, [
                'cate' => [
                    'required',
                    Rule::in([1, 2])
                ]

            ]
        );

        $userInfo = UserService::getUserInfo();
        $userId = $userInfo['uid'];

        $flag = $this->personalStrategy->setType($userId, $request->post('cate'));
        if(!$flag){
            return $this->toJson(-100);
        }
        return $this->success();
    }
    /**zwp
     * 设置员工职位
     * @param Request $request
     * @return array
     */
    public function setJOb(Request $request){
        $uid = $request->uid;
        $job = $request->job;
        //$uid = 2;
        //$job = 16;
        $flag = $this->personalStrategy->setJob($uid,$job);
        if(!$flag){
            return $this->toJson(-100);
        }
        return $this->success();
    }
    /**zwp
     * 写入员工基本信息
     * @param Request $request
     * @return type
     */
    public function baseInfo(Request $request){
        $uid = $request->uid;
        $data = $request->data;
        $flag = $this->personalStrategy->baseInfo($uid,$data);
        if(!$flag){
            return $this->toJson(-100);
        }
        return $this->success();
    }
    /**zwp
     * 获取员工基本信息
     * @param Request $request
     * @return type
     */
    public function getBaseInfo(Request $request){
        $uid = $request->uid;
        //$uid = 2;
        $data = $this->personalStrategy->getBaseInfo($uid);
        if(!$data){
            return $this->toJson(-3);
        }
        return $this->success($data);
    }
    /**zwp
     * 获取员工扩展信息
     * @param Request $request
     * @return type
     */
    public function getExtendInfo(Request $request){
        $uid = $request->uid;
        //$uid = 2;
        $data = $this->personalStrategy->getExtendInfo($uid);
        if(!$data){
            return $this->toJson(-3);
        }
        return $this->success($data);
    }
    /**zwp
     * 设置员工扩展信息
     * @param Request $request
     * @return type
     */
    public function setExtendInfo(Request $request){
        $uid = $request->uid;
        $data = $request->data;
        $flag = $this->personalStrategy->setExtendInfo($uid,$data);
        if(!$flag){
            return $this->toJson(-100);
        }
        return $this->success();
    }
    /**zwp
     * 工厂求职意向
     * @param Request $request
     * @return type
     */
    public function jobWill(Request $request){
        $uid = $request->uid;
        $data = $request->data;
        $flag = $this->personalStrategy->jobWill($uid,$data);
        if(!$flag){
            return $this->toJson(-100);
        }
        return $this->success();
    }
    /**zwp
     * 员工认证
     * @param Request $request
     * @return array
     */
    public function certification(Request $request){
        $uid = $request->uid;
        $data = $request->data;
        $flag = $this->personalStrategy->certification($uid,$data);
        if(!$flag){
            return $this->toJson(-100);
        }
        return $this->success();
    }
    /**zwp
     * 获取教育信息
     * @param Request $request
     * @return array
     */
    public function getEducation(Request $request){
        $uid = $request->uid;
        //$uid = 2;
        $data = $this->personalStrategy->getEducation($uid);
        if(!$data){
            return $this->toJson(-3);
        }
        return $this->success($data);
    }
    public function setInfo(Request $request){
        $uid = $request->uid;
        $data = $request->data;
        $flag = $this->personalStrategy->setInfo($uid,$data);
        if(!$flag){
            return $this->toJson(-100);
        }
        return $this->success();
    }
    /**zwp
     * 编辑教育信息
     * @param Request $request
     * @return type
     */
    /*
    public function setEducation(Request $request){
        $uid = $request->uid;
        $data = $request->data;
        $flag = $this->personalStrategy->setEducation($uid,$data);
        if(!$flag){
            return $this->toJson(-100);
        }
        return $this->success();
    }
    */
    public function getWorkHistory(Request $request){
        $uid = $request->uid;
        //$uid = 2;
        $data = $this->personalStrategy->getWorkHistory($uid);
        if(!$data){
            return $this->toJson(-3);
        }
        return $this->success($data);
    }
    /*
    public function setWorkHistory(Request $request){
        $uid = $request->uid;
        $data = $request->data;
        $flag = $this->personalStrategy->setWorkHistory($uid,$data);
        if(!$flag){
            return $this->toJson(-100);
        }
        return $this->success();
    }
    */
    public function getFamily(Request $request){
        $uid = $request->uid;
        //$uid = 2;
        $data = $this->personalStrategy->getFamily($uid);
        if(!$data){
            return $this->toJson(-3);
        }
        return $this->success($data);
    }
    /*
    public function setFamily(Request $request){
        $uid = $request->uid;
        $data = $request->data;
        $flag = $this->personalStrategy->setFamily($uid,$data);
        if(!$flag){
            return $this->toJson(-100);
        }
        return $this->success();
    }
    */
    public function getComment(Request $request){
        $uid = $request->uid;
        //$uid = 2;
        $data = $this->personalStrategy->getComment($uid);
        if(!$data){
            return $this->toJson(-3);
        }
        return $this->success($data);
    }
    public function getWillFactory(Request $request){
        $uid = $request->uid;
        //$uid = 2;
        $data = $this->personalStrategy->getWillFactory($uid);
        if(!$data){
            return $this->toJson(-3);
        }
        return $this->success($data);
    }
    public function getWillCompany(Request $request){
        $uid = $request->uid;
        $uid = 2;
        $data = $this->personalStrategy->getWillCompany($uid);
        if(!$data){
            return $this->toJson(-3);
        }
        return $this->success($data);
    }
    public function getCertification(Request $request){
        $uid = $request->uid;
        //$uid = 67;
        $data = $this->personalStrategy->getCertification($uid);
        if(!$data){
            return $this->toJson(-3);
        }
        return $this->success($data);
    }
    public function delPic(Request $request){
        $uid = $request->uid;
        //$uid = 67;
        $data = $request->data;
        $pic = $data["delPic"];
        //$dataPic["files"] = $data["files"];
        //$flag = $this->personalStrategy->delPic($uid,$dataPic);
        $delFlag = $this->delFile($pic);
        //if($flag){
            return $this->success($delFlag);
        //}
        //return $this->toJson(-100);
    }
    public function delFile($pic){
        $root = $this->getRootDir();
        $res = [];
        foreach ($pic as $v){
            $path = $root.$v;
            if(unlink($path)){
                $res[$v] = "删除成功"; 
            }else{
                $res[$v] = "删除失败";
            }
        }
        return $res;
    }
    public function getRootDir(){
        $root = __ROOT__;
        $root = str_replace("\\", "/", $root);
        return $root;
    }
    public function upCertification(Request $request){
        
        $type = $request->type;
        $selfs = [];
        $idcards = [];
        $byzs = [];
        $awards = [];
        $marrys = [];
        $drives = [];
        $resFile = Upload::uploadImgByApi($request,$type);
        $code = $resFile["code"];
        if($code!=0){
            return $this->toJson(-100);
        }
        switch ($type) {
            case "selfs":
                if(cache("selfs")==null){
                    array_push($selfs, $resFile["filePath"]);
                    cache("selfs",$selfs);
                }else{
                    $selfs = cache("selfs");
                    array_push($selfs, $resFile["filePath"]);
                }
                
                break;
            case "idcards":
                if(cache("idcards")==null){
                    array_push($idcards, $resFile["filePath"]);
                    cache("idcards",$idcards);
                }else{
                    $idcards = cache("idcards");
                    array_push($idcards, $resFile["filePath"]);
                }

                break;
            case "byzs":
                if(cache("byzs")==null){
                    array_push($byzs, $resFile["filePath"]);
                    cache("byzs",$byzs);
                }else{
                    $byzs = cache("byzs");
                    array_push($byzs, $resFile["filePath"]);
                }
                
                break;
            case "awards":
                if(cache("awards")==null){
                    array_push($awards, $resFile["filePath"]);
                    cache("awards",$awards);
                }else{
                    $awards = cache("awards");
                    array_push($awards, $resFile["filePath"]);
                }

                break;
            case "marrys":
                if(cache("marrys")==null){
                    array_push($marrys, $resFile["filePath"]);
                    cache("marrys",$marrys);
                }else{
                    $marrys = cache("marrys");
                    array_push($marrys, $resFile["filePath"]);
                }

                break;
            case "drives":
                if(cache("drives")==null){
                    array_push($drives, $resFile["filePath"]);
                    cache("drives",$drives);
                }else{
                    $drives = cache("drives");
                    array_push($drives, $resFile["filePath"]);
                }

                break;
            

            default:
                break;
        }
        $data = [
                    "selfs"=>$selfs,
                    "idcards"=>$idcards,
                    "byzs"=>$byzs,
                    "awards"=>$awards,
                    "marrys"=>$marrys,
                    "drives"=>$drives
                ];
        $dataRes["files"] = $data;
        cache("files",json_encode($data));
        $this->success($dataRes);
    }
    public function saveCertification(Request $request){
        $uid = $request->uid;
        $data = $request->data;
        $flag = $this->personalStrategy->saveCertification($uid,$data);
        if(!$flag){
            return $this->toJson(-100);
        }
        return $this->success();
    }
    public function getUserPhone(Request $request){
        $uid = $request->uid;
        //$uid = 1;
        $data = $this->personalStrategy->getUserPhone($uid);
        if(!$data){
            return $this->toJson(-3);
        }
        return $this->success($data);
    }
    public function staffSendYzm(Request $request){
        return $this->sendYzm($request);
    }

    public function savePassword(Request $request){
        $yzm = $request->yzm;
        $uid = $request->uid;
        $phone = $request->phone;
        $resYzm = $this->getYzm($phone);
        if(!($yzm==$resYzm)){
            return $this->toJson(-7);
        }
        $password = $request->password;
        $pwdHash = Hash::make($password);
        $flag = $this->personalStrategy->savePassword($uid,$phone,$pwdHash);
        if(!$flag){
            return $this->toJson(-100);
        }
        return $this->success();
    }
    public function getAcceptListsCount(Request $request){
        $uid = $request->uid;
        //$uid = 67;
        $pageSize = $request->pageSize;
        //$pageSize = 10;
        $count = $this->personalStrategy->getAcceptListsCount($uid);
        if(!$count){
            return $this->toJson(-3);
        }
        $data["count"] = $count;
        $data["page"] = ceil($count/$pageSize);
        return $this->success($data);
    }
    public function getAcceptListsByPage(Request $request){
        $uid = $request->uid;
        //$uid = 67;
        $pageSize = $request->pageSize;
        //$pageSize = 10;
        $page = $request->page;
        //$page = 1;
        $data = $this->personalStrategy->getAcceptListsByPage($uid,$pageSize,$page);
        if(!$data){
            return $this->toJson(-3);
        }
        return $this->success($data);
    }
    public function acceptInfo(Request $request){
        $id = $request->origin_id;
        $id = 62;
        $data = $this->personalStrategy->acceptInfo($id);
        if(!$data){
            return $this->toJson(-3);
        }
        return $this->success($data);
    }
}